﻿using System;
using System.Collections.Generic;
using System.Text;
using FloatingWindowTools.FloatingMapbasicWindow;
using FloatingWindowTools.NormalFloatingWindow;
using System.Windows.Forms;
using System.Reflection;
using MapinfoWrapper;
using MapinfoWrapper.Mapinfo;

namespace FloatingWindowTools
{
    public static class Entry
    {
        private static MapinfoSession mapinfo;
        private static MapinfoSession Mapinfo
        {
            get
            {
                if (mapinfo == null)
                {
                    mapinfo = MapinfoSession.GetLoadedInstance();
                }
                return mapinfo;
            }
        }

        public static void FloatMapbasicWindow()
        {
            Window mapbasicwindow = new Window(Mapinfo, (int)Window.WindowType.WIN_MAPBASIC);

            if (!mapbasicwindow.IsOpen)
            {
                // Open the mapbasic window.
                Mapinfo.Do("Run Menu Command 608");
            }

            MapbasicWindowUI window = new MapbasicWindowUI(mapbasicwindow,Mapinfo);
            window.FormClosed += new FormClosedEventHandler(window_FormClosed);

            Window mapinfoWindow = new Window(Mapinfo, (int)Window.WindowType.WIN_MAPINFO);
            window.Show(mapinfoWindow);
        }

        static void window_FormClosed(object sender, FormClosedEventArgs e)
        {
            Mapinfo.Do("Alter Button ID 2005 Enable");

            //TODO: Enable menu item not working right.
            //Mapinfo.Do("Alter Menu Item Load Enable");
        }

        public static void FloatWindow()
        {
            string ID = Mapinfo.Eval("FrontWindow()");
            Window windowtofloat = new Window(Mapinfo, Convert.ToInt32(ID));

            if (windowtofloat.Title == "TempLcParent" || !windowtofloat.IsOpen)
            {
                MessageBox.Show("There is no selected window");
                return;
            }

            FloatingWindow window = new FloatingWindow(windowtofloat);

            Window mapinfoWindow = new Window(Mapinfo, (int)Window.WindowType.WIN_MAPINFO);

            window.Show(mapinfoWindow);
        }

        public static void ShowAbout()
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }
    }
}
